package com.endpoint.swagger2demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment){
        Profiles of=Profiles.of("dev","test");
        boolean flag=environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("用户模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.endpoint.swagger2demo.controller"))
                .build()
                .enable(flag);
    }

    @Bean
    public Docket docket2(Environment environment){
        Profiles of=Profiles.of("dev","test");
        boolean flag=environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("作者模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.endpoint.swagger2demo.controller"))
                .paths(PathSelectors.ant("/author/**"))
                .build()
                .enable(flag);
    }

    @Bean
    public Docket docket3(Environment environment){
        Profiles of=Profiles.of("dev","test");
        boolean flag=environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("书籍模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.endpoint.swagger2demo.controller"))
                .paths(PathSelectors.ant("/book/**"))
                .build()
                .enable(flag);
    }

    //配置文档信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("cr","http://www.gec-edu.org","3268997699@qq.com");
        return new ApiInfo("标题-Swagger2学习",
                "描述-springboot项目使用Swagger2",
                "1.0",
                "组织链接-urn:tos",
                contact, //联系人信息
                "许可-Apache 2.0",
                "许可连接-http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());//扩展
    }
}
